﻿@model IList<DistanceLearning.Models.JournalLesson>
@using DistanceLearning
@using DistanceLearning.Models
@{
    var ajaxOptions = new AjaxOptions
                          {
                              UpdateTargetId = "user_details",
                              InsertionMode = InsertionMode.Replace,
                              HttpMethod = "POST",
                          }; }


<b>Расписание на @DateHelper.GetRussianValue(DateHelper.GetDayOfWeek(Model[0].DayOfWeek))</b>
@using(Ajax.BeginForm("CreateTimetable", ajaxOptions)) {
    @Html.ValidationSummary(true)
    for (int i = 0; i < Model.Count; ++i) {
        @Html.HiddenFor(model => model[i].Order)
        @Html.HiddenFor(model => model[i].JournalID)
        @Html.HiddenFor(model => model[i].DayOfWeek)
        <div>
            <b>Урок @(i+1)</b>
            @{
                var sujectAttributes = new Dictionary<string, object>();
                sujectAttributes.Add("id", string.Format("SubjectName{0}", i));
                sujectAttributes.Add("selectedIndex", i); // индекс элемента dropdown box [0-7], используется для выбора teachers во втором dropdown 
            }
            @Html.DropDownListFor(model => model[i].SubjectID, new SelectList(ViewBag.Subjects, "SubjectName", "SubjectName", Model[i].SubjectID), sujectAttributes)
            @*@Html.DropDownList(string.Format("[{0}].SubjectID", i), new SelectList(ViewBag.Subjects, "SubjectName", "SubjectName"), sujectAttributes)*@
            @{
                var teacherAttributes = new Dictionary<string, object>();
                teacherAttributes.Add("id", string.Format("TeacherID{0}", i));
            }

            <select name="@string.Format("[{0}].TeacherID", i)" id="@string.Format("TeacherID{0}", i)">
                @if(Model[i].TeacherID != 0)
                {
                    var teacherDisplayText = Model[i].TeacherSubject.Teacher.ToString();
                    <option value="@Model[i].TeacherID">@teacherDisplayText</option>
                }
            </select>
            
        </div>
    }
    <p>
        <input type="submit" value="Создать" />
    </p>
}

<script type="text/javascript">
    $(function () {
        // Фильтр по атрибутам: выбор всех select с id начинающимся на 'SubjectName'
        $("select[id^='SubjectName']").change(function () {
            debugger;
            var selectedIndex = this.getAttribute("selectedIndex");
            $.ajax({
                url: "/Administration/GetTeacherListBySubject",
                type: "POST",
                data: { subjectName: $(this).val() },
                success: function(teachers) {
                    debugger;
                    $("#TeacherID" + selectedIndex).empty();
                    $.each(teachers, function(i, teacher) {
                        $("#TeacherID" + selectedIndex).append('<option value="' + teacher.TeacherID +  '">' + teacher.DisplayText + "</option>");
                    });
                }
            });
        });
    });
</script>


